#![allow(dead_code)]
struct Point {
    x: f64,
    y: f64,
}

impl Point {
    // 实例化一个 point
    fn new(x: f64, y: f64) -> Point {
        Point { x, y }
    }
}

struct Rectangle {
    top_left: Point,
    bottom_right: Point,
}

impl Rectangle {
    // 周长
    fn perimeter(&self) -> f64 {
        let Point { x: left, y: top } = self.top_left;
        let Point {
            x: right,
            y: bottom,
        } = self.bottom_right;
        2_f64 * ((right - left) + (top - bottom))
    }

    // 面积
    fn area(&self) -> f64 {
        // 解构
        let Point { x: left, y: top } = self.top_left;
        let Point {
            x: right,
            y: bottom,
        } = self.bottom_right;
        // 计算面积
        let w = right - left;
        let h = bottom - top;
        w * h
    }
}

fn main() {
    println!("方法: 依附于对象的函数. 通过 self 访问中的数据和其他. 方法用 impl ");

    let rectangle = Rectangle {
        top_left: Point { x: 1_f64, y: 8_f64 },
        bottom_right: Point {
            x: 10_f64,
            y: 10_f64,
        },
    };
    println!("面积: {}", rectangle.area());
    println!("周长: {}", rectangle.perimeter());
}
